<template>
    <div>
        <div class="crumbs"></div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <!-- <el-select v-model="query.address" placeholder="地址" class="handle-select mr10">
                    <el-option key="1" label="广东省" value="广东省"></el-option>
                    <el-option key="2" label="湖南省" value="湖南省"></el-option>
                </el-select> -->
                <el-input v-model="query.order_code" placeholder="订单编号" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="order_id" label="ID" width="80" align="center"></el-table-column>
                <el-table-column prop="order_code" width="260" label="订单编号"></el-table-column>
                <el-table-column prop="shop_name"  label="门店"></el-table-column>
                <el-table-column label="产品数量" width="100">
                    <template slot-scope="scope">{{ scope.row.sum }}</template>
                </el-table-column>
                <el-table-column label="订单总价" width="100">
                    <template slot-scope="scope">￥{{ scope.row.total_price }}</template>
                </el-table-column>
                <el-table-column prop="order_pay" width="200" label="收单机构"></el-table-column>

                <el-table-column prop="create_time" label="订单生成时间" width="160"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleShow(scope.$index, scope.row)">查看</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.currentPage"
                    :page-size="query.pageSize"
                    :page-count="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 查看订单抽屉-->
        <el-drawer title="订单详情" :data="form" :visible.sync="drawer" :direction="direction" size="50%">
            <div class="container">
                <div class="handle-box">订单编号：{{ form.order_code }}</div>
                <div class="handle-box">收单机构：{{ form.order_pay }}</div>
                <div class="handle-box">订单生成时间：{{ form.create_time }}</div>
                

                <div class="table-mg-top">
                    <div class="handle-box">订单明细：</div>
                    <el-table :data="tableData.orderList" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                        <el-table-column prop="name" label="产品名称"></el-table-column>
                        <el-table-column label="产品数量" width="100">
                            <template slot-scope="scope">{{ scope.row.count }}</template>
                        </el-table-column>
                        <el-table-column label="产品小计" width="150">
                            <template slot-scope="scope">￥{{ scope.row.price }}</template>
                        </el-table-column>
                    </el-table>

                   
                </div>
                <div class="clearfix">
                    <div class="total-box">
                        <div class="total-box-label">商品总数：{{ form.sum }} 件</div>
                        <div class="total-box-label">价格总计：￥{{ form.total_price }} 元</div>
                    </div>
                </div>
            </div>
        </el-drawer>
    </div>
</template>

<script>
// import { fetchData } from '../../api/index';

import steambarcodereader from '@/components/common/steambarcodereader.vue';
export default {
    name: 'OrderTable',
    data() {
        return {
            query: {
                order_code: '',
                currentPage: 1,
                pageSize: 10
            },
            tableData: [],
            orderList: [
                {
                    product_name: '山海关',
                    counts: 1,
                    price: 3.99
                },
                {
                    product_name: '统一鲜橙多',
                    counts: 1,
                    price: 1.0
                }
            ],
            multipleSelection: [], //多选框数组
            delList: [], //批量删除
            editVisible: false,
            addVisible: false,
            drawer: false,
            direction: 'rtl',
            vilidateBarcodeMessage: '',
            pageTotal: 0,
            form: {
                barcode: '',
                order_id:1
            },
            idx: -1, //当前分页页面的第几行数据
            id: -1 //
        };
    },
    components: {
        //引入外部组件，这里是定义
        steambarcodereader
    },
    created() {

    },
    mounted(){
        this.InitData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        InitData() {
            this.getData();
        },
        async getData() {
            let res = await this.$http.post('getOrderByPager', this.query);
            console.log(res);
            this.tableData = res.data.data;
            this.pageTotal = res.data.pageTotal;
            // fetchData(this.query).then(res => {
            //     console.log(res);
            //     this.tableData = res.list;
            //     this.pageTotal = res.pageTotal || 50;
            // });
        },
        // 触发搜索按钮
        handleSearch() {
            //this.$set(this.query, 'pageIndex', 1);
            this.InitData();
        },
        // 删除操作
        handleDelete(index, row) {
            let ids = row.order_id;
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.saveDelete(ids);
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 删除方法封装
        async saveDelete(ids) {
            let res = await this.$http.post('deleteOrder', { ids: ids });
            if (res.data) {
                this.$message.success(`删除了${ids}`);
                this.InitData();
            } else {
                this.$message.error(`删除了${ids}`);
            }
        },
        // 批量删除
        delAllSelection() {
            const length = this.multipleSelection.length;
            let ids = '';
            //this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                ids += this.multipleSelection[i].order_id + ',';
            }
            ids = ids.substring(0, ids.length - 1);

            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //执行删除
                    this.saveDelete(ids);
                })
                .catch(() => {});
        },
        // 查看操作
        async handleShow(index, row) {
            // this.idx = index;
            this.form = row;
            // 获取order_id 请求查询详细信息
            this.form.order_id = row.order_id;
            let res = await this.$http.post('getOrderDetails', this.form);
            console.log(res);
            this.tableData.orderList=res.data;

            this.drawer = true;
        },
       
        // 分页导航
        handlePageChange(val) {
            // console.log(val);
            // this.$set(this.query, 'currentPage', val);
            this.query.currentPage = val;
            this.getData();
        },
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}

.table-mg-top {
    margin-top: 60px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

.total-box {
    margin: 70px 0 30px 0;
    width: 300px;
    float: right;
}

.total-box .total-box-label {
    text-align: left;
}

.clearfix:after {
	content: ".";
	display: block;
	visibility: hidden;
	height: 0;
	clear: both;
	overflow: hidden;
}
</style>
